package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketCoupon;
import com.powernobug.bean.model.*;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.CouponQueryParam;
import com.powernobug.mapper.MarketCouponMapper;

import com.powernobug.mapper.MarketCouponUserMapper;
import com.powernobug.service.AdminCouponService;
import com.powernobug.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @project: project1-no-bug
 * @package: com.powernobug.service
 * @author: HuangWeiLong
 * @date: 2024/8/22 22:41
 */
public class AdminCouponServiceImpl implements AdminCouponService {
    @Override
    public PageVo list(CouponQueryParam couponQueryParam) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketCouponMapper marketCouponMapper = sqlSession.getMapper(MarketCouponMapper.class);

        PageHelper.startPage(couponQueryParam.getPage(),couponQueryParam.getLimit());

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.setOrderByClause(couponQueryParam.getSort()+" "+couponQueryParam.getOrder());
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if(couponQueryParam!=null){
            if(couponQueryParam.getName()!=null&&couponQueryParam.getName().length()>0){
                criteria.andNameLike("%"+couponQueryParam.getName()+"%");
            }
            if(couponQueryParam.getType()!=null&&couponQueryParam.getType()>=0){
                criteria.andTypeEqualTo(couponQueryParam.getType());
            }
            if(couponQueryParam.getStatus()!=null&&couponQueryParam.getStatus()>=0){
                criteria.andStatusEqualTo(couponQueryParam.getStatus());
            }
        }
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        return PageVo.list(marketCoupons);
    }

    @Override
    public BaseRespVo update(MarketCoupon marketCoupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketCouponMapper marketCouponMapper = sqlSession.getMapper(MarketCouponMapper.class);

        marketCoupon.setUpdateTime(new Date());
        marketCouponMapper.updateByPrimaryKey(marketCoupon);
        return BaseRespVo.ok(marketCoupon);
    }

    @Override
    public void delete(MarketCoupon marketCoupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketCouponMapper marketCouponMapper = sqlSession.getMapper(MarketCouponMapper.class);
        marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
    }

    @Override
    public BaseRespVo create(MarketCoupon marketCoupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketCouponMapper marketCouponMapper = sqlSession.getMapper(MarketCouponMapper.class);
        marketCoupon.setAddTime(new Date());
        marketCouponMapper.insert(marketCoupon);
        return BaseRespVo.ok(marketCoupon);
    }

    @Override
    public BaseRespVo read(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketCouponMapper marketCouponMapper = sqlSession.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        return BaseRespVo.ok(marketCoupon);
    }

    @Override
    public List<MarketCouponUser> listuser(Integer couponId,String order,String sort) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketCouponUserMapper marketCouponUserMapper = sqlSession.getMapper(MarketCouponUserMapper.class);

        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        marketCouponUserExample.setOrderByClause(sort+" "+order);
        marketCouponUserExample.createCriteria().andCouponIdEqualTo(couponId);
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        return marketCouponUsers;
    }
}
